package cn.edu.ujn.lishlab3.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.lishlab3.dao.Customer;
import cn.edu.ujn.lishlab3.dao.CustomerMapper;
import cn.edu.ujn.util.Page;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements ICustomerService {
	@Autowired
	private CustomerMapper customerDao;

	@Override
	public Page<Customer> findCustomer(Integer page, Integer rows, String custName, String custSource,
			String custIndustry, String custLevel) {
		// TODO Auto-generated method stub
		
		Customer customer = new Customer();
		//判断客户名称、信息来源、行业、级别
		if(StringUtils.isNotBlank(custName)) {
			customer.setCust_name(custName);
		}
		if(StringUtils.isNotBlank(custSource)) {
			customer.setCust_source(custSource);
		}
		if(StringUtils.isNotBlank(custIndustry)) {
			customer.setCust_industry(custIndustry);
		}
		if(StringUtils.isNotBlank(custLevel)) {
			customer.setCust_level(custLevel);
		}
		
		customer.setStart((page-1)*rows);//当前页
		customer.setRows(rows);//每页数
		
		//查询客户列表
		List<Customer> customers = 
				customerDao.selectCustomerList(customer);
		//查询客户列表总记录数
	    Integer count = 
	    		customerDao.selectCustomerListCount(customer);
	    
	    //创建page返回对象
	    Page<Customer> result = new Page<>();
	    result.setPage(page);
	    result.setRows(customers);
	    result.setSize(rows);
	    result.setTotal(count);    
		
		return result;
	}

	@Override
	public int deleteByPrimaryKey(Integer custId) {
		// TODO Auto-generated method stub
		return this.customerDao.deleteByPrimaryKey(custId);
	}

	@Override
	public int insert(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.insert(row);
	}

	@Override
	public int insertSelective(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.insertSelective(row);
	}

	@Override
	public Customer selectByPrimaryKey(Integer custId) {
		// TODO Auto-generated method stub
		return this.customerDao.selectByPrimaryKey(custId);
	}

	@Override
	public int updateByPrimaryKeySelective(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.updateByPrimaryKeySelective(row);
	}

	@Override
	public int updateByPrimaryKey(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.updateByPrimaryKey(row);
	}

	@Override
	public List<Customer> selectCustomerList(Customer customer) {
		// TODO Auto-generated method stub
		
		return this.customerDao.selectCustomerList(customer);
	}

	@Override
	public Integer selectCustomerListCount(Customer customer) {
		// TODO Auto-generated method stub
		return this.customerDao.selectCustomerListCount(customer);
	}

	@Override
	public int createCustomer(Customer customer) {
		// TODO Auto-generated method stub
		
		return customerDao.createCustomer(customer);
	}

	@Override
	public Customer getCustomerById(Integer id) {
		// TODO Auto-generated method stub
		return customerDao.getCustomerById(id);
	}

	@Override
	public int updateCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return  customerDao.updateCustomer(customer);
	}

	@Override
	public int deleteCustomer(Integer id) {
		// TODO Auto-generated method stub
		return customerDao.deleteCustomer(id);
	}

}
